<template>
	<view>
		<cu-custom :isBack="true" bgColor="bg-blue">
			<block slot="content">修改密码</block>
			<template slot="backText">
				<text class="back" @tap="beforePage"></text>
			</template>
		</cu-custom>
		<view class="fromBox">
			<view class="labelPass">
				<input type="text" value="" v-model="before" confirm-type="done" placeholder="请输入原密码"/>
			</view>
			<view class="labelPass">
				<input type="text" value="" v-model="newpass" confirm-type="done" placeholder="请输入密码"/>
			</view>
			<view class="labelPass">
				<input type="text" value="" v-model="password" confirm-type="done" placeholder="请确认密码"/>
			</view>
			<view class="subinfo">
				<button type="primary" @tap="passSub" class="subBtn">确认</button>
			</view>
		</view>
	</view>
</template>

<script>
	import {post} from "../../lib/Util.js"
	export default {
		data() {
			return {
				before: '',
				newpass: '',
				password: '',
				userPass: null,
			}
		},
		onLoad() {
			this.yanzheng()
		},
		methods: {
			beforePage() {
				uni.navigateBack({
					delta: 1
				});
			},
			async passSub() {
				if (this.before ==='' ||this.newpass==='' ||this.password==='') {
					uni.showToast({
						icon:'none',
						title:'请输入密码'
					})
					return;
				}
				if(this.userPass !== this.before){
					console.log('this.userPass', this.userPass)
					console.log('this.before', this.before)
					uni.showToast({
						icon:'none',
						title:'原密码有误'
					})
					return;
				}
				if(this.newpass != this.password){
					uni.showToast({
						icon:'none',
						title:'俩次密码不一致'
					})
					return;
				}
				
				let id = uni.getStorageSync('userID')
				let data = {
					data:{
						id: id,
						username: this.$store.state.userName,
						password: this.password
					}
				}
				let res =await post('/user/update',data);
				if(res.msg ==='success' && res.data.password !='' && res.code == 0){
					uni.showToast({
						icon:'none',
						title:'修改成功,请重新登录'
					})
					setTimeout(()=> {
						uni.clearStorageSync()
						uni.reLaunch({
							url: '/pages/login/login'
						})
					}, 2000)
				}
			},
			async yanzheng(){
				let id = uni.getStorageSync('userID')
				let userid = {
					id:id
				}
				let res =await post('/user/findUserById',userid);
				if( res.code == 0 ){
					this.userPass = res.data.password
				}
			}
		}
	}
</script>

<style>
	.fromBox{
		padding: 40rpx 0 0 0;
		background: #f7f6fc;
		height: 100vh;
	}
	.labelPass{
		height: 100rpx;
		border-bottom: 1px solid #F2F2F2;
		padding: 25rpx 30rpx;
		box-sizing: border-box;
		background: #FFFFFF;
	}
	.subinfo{
		margin-top: 30rpx;
		padding: 0 30rpx;
		box-sizing: border-box;
	}
	.subBtn{
		background: #01cffe;
		color: #FFFFFF;
	}
</style>
